package io.github.eric.urpc.client;

import io.github.eric.urpc.core.rpc.RpcContext;
import io.github.eric.urpc.core.rpc.RpcMethod;
import io.github.eric.urpc.core.rpc.RpcController;
import io.github.eric.urpc.core.rpc.msg.ApplicationInfoRes;
import io.github.eric.urpc.core.rpc.msg.HeartBeat;

public class ClientCoreRpcController implements RpcController {
    private final ClientContext clientContext;

    ClientCoreRpcController(ClientContext clientContext) {
        this.clientContext= clientContext;
    }

    @RpcMethod
    public void onHeartBeat(HeartBeat msg) {
        if (clientContext.getLogger().isDebugEnabled()) {
            clientContext.getLogger().debug("[urpc client] client receive a HeartBeat, sendAt: {}", msg.getSendAt());
        }
    }

    @RpcMethod
    public void onApplicationInfoRes(ApplicationInfoRes applicationInfoRes, RpcContext rpcContext) {
        boolean succeed = applicationInfoRes.getError() == null;
        if (!succeed) {
            clientContext.getLogger().error("[urpc client] register application info to server side error, caused from server '{}', please check details on server side",
                    applicationInfoRes.getError());
        } else {
            clientContext.getLogger().info("[urpc client] register application info to server side complete");
        }
        clientContext.getConnectionManager().updateConnectionInitStatus(rpcContext.getConnId(), applicationInfoRes.getError() == null);
    }
}
